﻿'Double click: Open history in grid, calculate growth per day/month/year
'Run with /now: write to DataGrowthLog.csv in same dir as executable then exit
'
'Form buttons:
'-log now - take a new reading, write it to disk, refresh grid to include new entry
'-schedule monthly - create new windows scheudled task to log data monthly
'-Launch log in Excel
Imports System.IO
Imports System.Data.SqlClient
Imports System.ComponentModel

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Dim a As Integer
        Dim cdrive As System.IO.DriveInfo
        Dim arg, args(10) As String

        args = Environment.GetCommandLineArgs
        For Each arg In args
            If LCase(arg) = "/now" Then
                newEntry()
                End
            End If
        Next

        'populate datagridview headers
        DataGridView1.ColumnCount = 11
        DataGridView1.Columns.Item(0).Name = "Linux Time"
        DataGridView1.Columns.Item(0).Visible = False
        DataGridView1.Columns.Item(1).HeaderText = "Date"
        DataGridView1.Columns.Item(2).HeaderText = "Drive"
        DataGridView1.Columns.Item(3).HeaderText = "Total Size"
        DataGridView1.Columns.Item(4).HeaderText = "Used Space"
        DataGridView1.Columns.Item(5).HeaderText = "Free Space"
        DataGridView1.Columns.Item(6).HeaderText = "New Growth"
        DataGridView1.Columns.Item(7).HeaderText = "Days Since Last Check"
        DataGridView1.Columns.Item(8).HeaderText = "30 day Growth Rate"
        DataGridView1.Columns.Item(9).HeaderText = "Cumulative 30 Day Growth Rate"
        DataGridView1.Columns.Item(10).HeaderText = "Cumulative Yearly Growth Rate"

        'populate drive select combo box
        For a = 65 To 90
            cdrive = My.Computer.FileSystem.GetDriveInfo(Chr(a) & ":\")
            If cdrive.DriveType = 3 Then
                ComboBox1.Items.Add(cdrive.Name)
            End If
        Next
        ComboBox1.Text = "C:\"
        Timer1.Interval = 250
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        newEntry()
    End Sub

    Private Sub newEntry()
        Dim cdrive As System.IO.DriveInfo
        Dim TotalSize, TotalFree, TotalUsed As Double
        Dim lTime As TimeSpan = Date.Now - Date.Parse("01/01/1980 00:00:00")

        Try
            Dim objWriter As IO.StreamWriter
            objWriter = IO.File.AppendText("c:\temp\DGA_log.csv")
            For a = 65 To 90
                cdrive = My.Computer.FileSystem.GetDriveInfo(Chr(a) & ":\")
                If cdrive.DriveType = 3 Then
                    TotalSize = Val(cdrive.TotalSize) / 1024 / 1024 / 1024
                    TotalFree = Val(cdrive.TotalFreeSpace) / 1024 / 1024 / 1024
                    TotalUsed = TotalSize - TotalFree
                    TotalUsed = TotalUsed
                    objWriter.Write(lTime.TotalMilliseconds.ToString & "," & Date.Today & "," & cdrive.Name & "," & TotalSize & "," & TotalUsed & "," & TotalFree & Chr(13))
                End If
            Next
            objWriter.Close()
            refreshGrid()
        Catch ex As Exception
            Dim objWriter = IO.File.Create("c:\temp\DGA_log.csv")
            objWriter.Close()
            newEntry()
        End Try
    End Sub

    

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        DataGridView1.Width = Me.Width - 40
        DataGridView1.Height = Me.Height - 70

    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
        refreshGrid()
    End Sub

    Private Sub refreshGrid()
        DataGridView1.Rows.Clear()
        Dim Record() As String
        Dim z, lr As Integer
        Dim lastused, current, change, nd, ld, dd, c, cc As Double
        Try
            Dim sReader As New StreamReader("c:\temp\DGA_log.csv")
            While sReader.Peek() <> -1
                Record = sReader.ReadLine().Split(",")
                If Record(2) = ComboBox1.Text.ToString Then
                    DataGridView1.Rows.Add(Record)
                End If
            End While
            sReader.Close()
        Catch ex As Exception
            MsgBox("No data found. Capture some data.")
        End Try
        DataGridView1.Sort(DataGridView1.Columns(0), ListSortDirection.Descending)
        lr = DataGridView1.Rows.Count - 1
        For z = 0 To lr
            If z < lr - 1 Then
                'calculate data change quantity
                lastused = DataGridView1(4, z + 1).Value
                current = DataGridView1(4, z).Value
                change = current - lastused
                DataGridView1(6, z).Value = Rnd(change)

                'calculate days since last check
                nd = DataGridView1(0, z).Value
                ld = DataGridView1(0, z + 1).Value
                dd = (nd - ld) / 1000 / 60 / 60 / 24
                DataGridView1(7, z).Value = Rnd(dd)

                'calculate 30 day growth rate
                DataGridView1(8, z).Value = Rnd((change * 30) / dd)

                'calculate cumulative 30 day growth rate
                c = 0
                cc = 0
                For x = z To lr
                    c = c + DataGridView1(8, z).Value
                    cc = cc + 1
                Next
                DataGridView1(9, z).Value = Rnd(c / cc)

                'calculate yearly growth rate

                DataGridView1(10, z).Value = DataGridView1(9, z).Value * 12
            End If
        Next
        resizeForm()

    End Sub
    Sub resizeForm()
        Me.Height = DataGridView1.Height + 70
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim args(10) As String
        Dim path, fileName As String

        path = Directory.GetCurrentDirectory
        fileName = Application.ExecutablePath
        args = Environment.GetCommandLineArgs
        
        Process.Start("c:\windows\system32\schtasks.exe", "/create /F /SC MONTHLY /MO first /d MON /tn DataGrowthAssistant /TR c:\temp\datagrowthassistant.exe /now")
        MsgBox("Scheduled task 'DataGrowthAssistant' created. Task scheduler will now launch. Modify the task to run without being logged in and enter your credentials to complete the process.")
        Process.Start("c:\windows\system32\control.exe", "schedtasks")



    End Sub

    Private Sub Label1_Click(sender As Object, e As EventArgs) Handles Label1.Click

    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Process.Start("http://bit.ly/1LQy8XQ")
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        MsgBox("This program tracks data growth by recording to a csv file in c:\temp then parsing/sorting/filtering the CSV file when the program is opened. Use the scheduled task button to create a monthly scheduled task. You can also record data at will with the 'New Entry' button or /now switch if launched from the command line.")

    End Sub
    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Process.Start("mailto:phxxxx@gmail.com?subject=DGA Feedback")

    End Sub

    Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub
End Class
